# from rest_framework_jwt.views import JSONWebTokenAPIView
# from rest_framework_jwt.serializers import JSONWebTokenSerializer
# from django.contrib.auth import authenticate
# from rest_framework import serializers
# from django.utils.translation import ugettext as _
# from rest_framework_jwt.settings import api_settings
# jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
# jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
from django.contrib.auth import authenticate
from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework_jwt.serializers import JSONWebTokenSerializer, jwt_encode_handler, jwt_payload_handler
from django.utils.translation import ugettext as _
from rest_framework_jwt.views import JSONWebTokenAPIView

from apps.users.models import User


class SupperUserLoginSerializer(JSONWebTokenSerializer):

  def validate(self, attrs):
    credentials = {
      self.username_field: attrs.get(self.username_field),
      'password': attrs.get('password')
    }

    if all(credentials.values()):
      user = authenticate(**credentials)

      if user:
        if not user.is_active:
          msg = _('User account is disabled.')
          raise serializers.ValidationError(msg)

        # ==================添加用户的判断========================

        if not user.is_staff:
          msg = _('User account is disabled.')
          raise serializers.ValidationError(msg)

        payload = jwt_payload_handler(user)

        return {
          'token': jwt_encode_handler(payload),
          'user': user,
        }
      else:
        msg = _('Unable to log in with provided credentials.')
        raise serializers.ValidationError(msg)
    else:
      msg = _('Must include "{username_field}" and "password".')
      msg = msg.format(username_field=self.username_field)
      raise serializers.ValidationError(msg)


class SupperUserLoginView(JSONWebTokenAPIView):
  """
  API View that receives a POST with a user's username and password.

  Returns a JSON Web Token that can be used for authenticated requests.
  """
  serializer_class = SupperUserLoginSerializer


admin_jwt_token = SupperUserLoginView.as_view()


# class SupperUserLogin(APIView):
#   def post(self, request):
#     user = request.data.get.all()
#     pass
